
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Demo," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>引入计算属性、action、动态内容 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        引入计算属性、action、动态内容
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:36.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Demo/" rel="tag">Ember-Demo</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 7 分钟</span>
    </header>

    <div class="post-content">
      <p>来源：<a target="_blank" rel="noopener" href="http://yoember.com/">yoember.com</a><br>作者：<a target="_blank" rel="noopener" href="http://zoltan.nz/">Zoltan</a></p>
<p><strong>声明</strong>：<em>本文的转载与翻译是经过作者认可的，再次感谢原作，如有侵权请给我留言，我会删除博文！！</em> 希望本系列教程能帮助更多学习Ember.js的初学者。</p>
<p>接着前面一篇：</p>
<ol>
<li><a href="http://xcoding.tech/2016/03/30/Ember-Demo/%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%E4%BB%A5%E5%8F%8A%E4%BD%BF%E7%94%A8Ember.js%E5%88%9B%E5%BB%BA%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%9D%99%E6%80%81%E9%A1%B5%E9%9D%A2/">环境搭建以及使用Ember.js创建第一个静态页面</a></li>
</ol>
<h2 id="美化主页，增加邮件输入框"><a href="#美化主页，增加邮件输入框" class="headerlink" title="美化主页，增加邮件输入框"></a>美化主页，增加邮件输入框</h2><p>在主页中增加一个<a target="_blank" rel="noopener" href="http://www.bootcss.com/">Bootstrap</a>的<code>jumbotron</code>，在这个<code>jumbotron</code>组件中增加一个<code>input</code>输入框和一个<code>button</code>按钮。</p>
<h3 id="在首页index-hbs中增加静态HTML代码"><a href="#在首页index-hbs中增加静态HTML代码" class="headerlink" title="在首页index.hbs中增加静态HTML代码"></a>在首页index.hbs中增加静态HTML代码</h3><p>为了界面的美化在HTML代码中使用了很多<a target="_blank" rel="noopener" href="http://www.bootcss.com/">Bootstrap</a>的样式，更多有关Bootstrap的使用请自行学习。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;jumbotron text-center&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Coming Soon<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>Don&#x27;t miss our launch date, request an invitation now.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;form-horizontal form-group form-group-lg row&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-xs-10 col-xs-offset-1 col-sm-6 col-sm-offset-1 col-md-5 col-md-offset-2&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;email&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;Please type your e-mail address.&quot;</span> <span class="attr">autofocus</span>=<span class="string">&quot;autofocus&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-xs-10 col-xs-offset-1 col-sm-offset-0 col-sm-4 col-md-3&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">button</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-primary btn-lg btn-block&quot;</span>&gt;</span>Request invitation<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>等待项目重启完成，可以在首页看到如下效果页面：</p>
<p><img src="/image/blog-image/90.png" alt="首页效果截图"></p>
<h2 id="计算属性"><a href="#计算属性" class="headerlink" title="计算属性"></a>计算属性</h2><p>计算属性简单讲它就是一个特殊点的JS函数。如果你看过<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/03/17/ember-js-ru-men-zhi-nan-ji-suan-shu-xing-compute-properties/">Ember.js 入门指南之三计算属性（compute properties）</a>相信使用起来会比较简单，再次不过多介绍。</p>
<h3 id="计算属性使用"><a href="#计算属性使用" class="headerlink" title="计算属性使用"></a>计算属性使用</h3><p>下面几点需求可以通过计算属性去实现：</p>
<ol>
<li>当输入框的为空时按钮“Request invitation”不可用</li>
<li>当输入的邮箱号码格式不正确时按钮“Request invitation”不可用</li>
<li>点击按钮“Request invitation”之后显示响应信息</li>
<li>数据保存完成之后情况邮箱输入框的内容</li>
</ol>
<h3 id="isDisabled属性"><a href="#isDisabled属性" class="headerlink" title="isDisabled属性"></a>isDisabled属性</h3><p>既然介绍了计算属性那么应该拿来用了！我们使用属性<code>isDisabled</code>控制按钮“Request invitation”是否可用。在<code>button</code>标签上增加一个HTML属性<code>disabled</code>，这个HTML属性决定了按钮<code>button</code>是否可用。当HTML属性<code>disabled=true</code>时按钮不可用，当HTML属性<code>disabled=false</code>时按钮可用，那么如何控制这个值是<code>true</code>还是<code>false</code>呢？别忘了在Handlebars模板中可以直接使用<code>{{}}`表达式获取属性的值，下面修改模板`index.hbs`，在标签`button`中增加属性`disabled`的设置：
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-primary btn-lg btn-block&quot;</span> <span class="attr">disabled</span>=<span class="string">&#123;&#123;isDisabled&#125;&#125;</span>&gt;</span>Request invitation<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>
那要在哪里控制`isDisabled`的值呢？目前有2种方式，第一是在路由`route`中控制这个值，另外一种是在控制器`controller`中控制这个属性的值。有关路由的信息在前一篇已经简单介绍过，或者看[Ember.js 入门指南之二十路由定义](http://blog.ddlisting.com/2016/03/25/ember-js-ru-men-zhi-nan-zhi-er-shi-lu-you-ding-yi/)学习。与路由同理，每个模板都对应有一个同名的控制器`controller`，如果你学习过MVC模式那么你应该很清楚什么是控制器，Ember中的控制器作用于MVC模式中的控制器相似，不过需要注意的是从`Ember 3.0`之后控制器将不再支持，所以呢！会在后面用组件替代控制器，官方也是这么推荐的！更多有关控制器的介绍请看[Controller Introduction](https://guides.emberjs.com/v2.4.0/controllers/)。
同样的我们仍然是使用Ember CLI创建控制器，命令如下：
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ember g controller index</span><br></pre></td></tr></table></figure>
创建好控制器之后，在控制器内添加设置属性`isDisabled`的代码：
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/controller/index.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    isDisabled: <span class="literal">true</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
等待项目重启完毕，可以看到按钮是不可用，如果你把属性`isDisabled`设置为`false`那么按钮是可用的。

## 计算属性与观察者

计算属性和观察者是Ember非常重要的特性。更多有关它们的特性请看下面的文章：

1. [Ember.js 入门指南之三计算属性](http://blog.ddlisting.com/2016/03/17/ember-js-ru-men-zhi-nan-ji-suan-shu-xing-compute-properties/)
2. [计算属性官方参考文档](https://guides.emberjs.com/v2.4.0/object-model/computed-properties/)
3. [Ember.js 入门指南之四观察者](http://blog.ddlisting.com/2016/03/17/ember-js-ru-men-zhi-nan-guan-cha-zhe-observer/)
4. [观察者官方参考文档](https://guides.emberjs.com/v2.4.0/object-model/observers/)

在下面的代码中有关计算属性部分使用的`2.0`之后的语法，在`2.0`之前计算属性的语法是不一样的（[旧语法](https://guides.emberjs.com/v1.12.0/object-model/computed-properties/)）。

修改模板`index.hbs`，把邮箱号码输入框改为Ember的`{{input}}</code>助手。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- &lt;input type=&quot;email&quot; class=&quot;form-control&quot; placeholder=&quot;Please type your e-mail address.&quot; autofocus=&quot;autofocus&quot;/&gt; --&gt;</span></span><br><span class="line">&#123;&#123;input type=&quot;email&quot; value=emailAddress class=&quot;form-control&quot; placeholder=&quot;Please type your e-mail address.&quot; autofocus=&quot;autofocus&quot;&#125;&#125;</span><br></pre></td></tr></table></figure>
<p>等待项目重启之后可以看到界面并没有变化。<code>{{input}}</code>起到与原来代码同样的作用。<br><br>值得注意的是<code>value=emailAddress</code>，并不是<code>value=&quot;emailAddress&quot;</code>。你可以在控制器中通过名字<code>emailAddress</code>获取输入框的值。如果是<code>value=&quot;emailAddress&quot;</code>这种方式，输入框的值默认一直都是”emailAddress”，并且在控制器中不能使用属性<code>emailAddress</code>获取值。这一点与平常我们获取<code>&lt;input&gt;</code>输入框的值有差别，通常获取<code>&lt;input&gt;</code>输入框的值是通过<code>name</code>属性获取的。修改控制器代码，在控制器中增加个计算属性和一个观察器，以及一个普通属性<code>emailAddress</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/controller/index.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    isDisabled: <span class="literal">true</span>, <span class="comment">//设置默认值为true</span></span><br><span class="line">    emailAddress: <span class="string">&#x27;&#x27;</span>,  <span class="comment">// 设置默认值为空字符串</span></span><br><span class="line">    <span class="comment">// 定义一个计算属性，当属性emailAddress发生变化时会被执行不是主动执行的，是要有人调用才执行，</span></span><br><span class="line">    <span class="comment">// 比如执行：this.get(&#x27;actualEmailAddress&#x27;)去调用这个属性才会执行</span></span><br><span class="line">    actualEmailAddress: Ember.computed(<span class="string">&#x27;emailAddress&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;actualEmailAddress function is called: &#x27;</span>, <span class="built_in">this</span>.get(<span class="string">&#x27;emailAddress&#x27;</span>));</span><br><span class="line">    &#125;),</span><br><span class="line">    <span class="comment">// 定义一个观察器，当属性emailAddress发生变化时会自动执行，也就是说观察器会检测属性emailAddress值的变化</span></span><br><span class="line">    emailAddressChanged: Ember.observer(<span class="string">&#x27;emailAddress&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;observer is called: &#x27;</span>, <span class="built_in">this</span>.get(<span class="string">&#x27;emailAddress&#x27;</span>));</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>下面我们做一个非常有趣的小测试。<br><br>等待页面刷新完毕，打开浏览器控制台，选择标签<code>Ember</code>，在选择左侧的<code>/# Route</code>，找到<code>Controller</code>中名为<code>index</code>的，点击<code>$E</code>（如下图红色框出位置），然后再回到<code>Console</code>标签下。</p>
<p><img src="/image/blog-image/91.png" alt="控制台"></p>
<p>点击<code>$E</code>在<code>Console</code>下可以看到类似<code>Ember Inspector ($E):  Class &#123;__ember1459491972481: &quot;ember470&quot;, __ember_meta__: Meta&#125;</code>的信息。然后在控制台命令输入行输入<code>$E.get(&#39;actualEmailAddress&#39;)</code>代码的作用是获取计算属性的值。可以看到触发了计算属性方法，打印了日志，如下截图所示：</p>
<p><img src="/image/blog-image/92.png" alt="计算属性执行日志"></p>
<p>然后再次执行<code>$E.get(&#39;actualEmailAddress&#39;)</code>计算属性方法不会被执行，因为计算属性检测的属性<code>emailAddress</code>值并没有发生变化，没有发生变化，计算属性方法不会被执行，手动修改输入框的值，结果可以看到计算属性方法再次执行了，如下图所示：</p>
<p><img src="/image/blog-image/94.png" alt="修改输入框的值执行结果截图"></p>
<p>然后在控制台命令行在输入<code>$E.set(&#39;emailAddress&#39;, &#39;example@example.com&#39;)</code>这句代码意思是修改输入框的值。可以看到观察器方法执行了，因为观察器检测到被检测的属性<code>emailAddress</code>发生了变化，只要被检测的属性发生了变化就会自动执行。可以看到如下截图的日志信息：</p>
<p><img src="/image/blog-image/93.png" alt="观察器执行结果"></p>
<p>并且可以看到邮箱号码输入框的值被置为<code>example@example.com</code>。然后在控制台命令行再次输入<code>$E.set(&#39;emailAddress&#39;, &#39;example@example.com&#39;)</code>观察器方法并不会执行了，即使你输入多次也不会执行，因为你输入的值<code>example@example.com</code>始终没有变化。如果你稍微修改输入的值那么可以看到观察器又执行了。比如输入<code>$E.set(&#39;emailAddress&#39;, &#39;test&#39;)</code>，可以看到控制台再次打印了日志信息。</p>
<p>测试观察器还有另外一种简单的方法，就是直接在邮件输入框直接输入某些内容。可以看到控制台会随着这输入的内容变化而变化，感觉就像是在检测键盘事件一样。下图是我输入<code>12@qq.com</code>控制台打印的日志信息：</p>
<p><img src="/image/blog-image/95.png" alt="输入12@qq.com日志信息"></p>
<p>到此，我想你对计算属性和观察者应该有了一定的认识了！！</p>
<h2 id="用计算属性修改isDisabled"><a href="#用计算属性修改isDisabled" class="headerlink" title="用计算属性修改isDisabled"></a>用计算属性修改isDisabled</h2><p>明白了计算属性之后，用计算属性重写<code>isDisabled</code>。控制器<code>index.js</code>代码修改如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/controller/index.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    <span class="comment">// isDisabled: true, //设置默认值为true</span></span><br><span class="line"></span><br><span class="line">    emailAddress: <span class="string">&#x27;&#x27;</span>,  <span class="comment">// 设置默认值为空字符串</span></span><br><span class="line"></span><br><span class="line">    isDisabled: Ember.computed(<span class="string">&#x27;emailAddress&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span> === <span class="built_in">this</span>.get(<span class="string">&#x27;emailAddress&#x27;</span>);  <span class="comment">//判断输入框内容是否为空</span></span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义一个计算属性，当属性emailAddress发生变化时会被执行不是主动执行的，是要有人调用才执行，</span></span><br><span class="line">    <span class="comment">// 比如执行：this.get(&#x27;actualEmailAddress&#x27;)去调用这个属性才会执行</span></span><br><span class="line">    <span class="comment">// actualEmailAddress: Ember.computed(&#x27;emailAddress&#x27;, function() &#123;</span></span><br><span class="line">    <span class="comment">//     console.log(&#x27;actualEmailAddress function is called: &#x27;, this.get(&#x27;emailAddress&#x27;));</span></span><br><span class="line">    <span class="comment">// &#125;),</span></span><br><span class="line">    <span class="comment">// 定义一个观察器，当属性emailAddress发生变化时会自动执行，也就是说观察器会检测属性emailAddress值的变化</span></span><br><span class="line">    <span class="comment">// emailAddressChanged: Ember.observer(&#x27;emailAddress&#x27;, function() &#123;</span></span><br><span class="line">    <span class="comment">//     console.log(&#x27;observer is called: &#x27;, this.get(&#x27;emailAddress&#x27;));</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>直接把简单属性<code>isDisabled</code>定义为计算属性，并且这个计算属性检测<code>emailAddress</code>值的变化，如果<code>emailAddress</code>值为空那么计算属性<code>isDisabled</code>的值为<code>true</code>否则值为<code>false</code>。从而实现判断按钮“Request invitation”是否可用。Ember封装了很多字符串判断方法，直接调用Ember封装好的现成的方法，代码再修改如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/controller/index.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    <span class="comment">// isDisabled: true, //设置默认值为true</span></span><br><span class="line"></span><br><span class="line">    emailAddress: <span class="string">&#x27;&#x27;</span>,  <span class="comment">// 设置默认值为空字符串</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// isDisabled: Ember.computed(&#x27;emailAddress&#x27;, function() &#123;</span></span><br><span class="line">    <span class="comment">//     return &#x27;&#x27; === this.get(&#x27;emailAddress&#x27;);  //判断输入框内容是否为空</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line"></span><br><span class="line">    isDisabled: Ember.computed.empty(<span class="string">&#x27;emailAddress&#x27;</span>)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>更多有关计算属性封装好的方法请看<a target="_blank" rel="noopener" href="http://emberjs.com/api/classes/Ember.computed.html">EMBER.COMPUTED NAMESPACE</a>。</p>
<h3 id="isValid"><a href="#isValid" class="headerlink" title="isValid"></a>isValid</h3><p>记得前面“计算属性使用”这个小结提出了使用计算属性实现多个需求，其中有一个是实现判断输入的邮箱号码是否是正确格式的邮箱。现在再增加一个计算属性<code>isValid</code>判断输入的邮箱号码的格式是否正确。然后再把这个计算属性绑定到原来的计算属性<code>isDisabled</code>上。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/controller/index.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line"></span><br><span class="line">    emailAddress: <span class="string">&#x27;&#x27;</span>,  <span class="comment">// 设置默认值为空字符串</span></span><br><span class="line"></span><br><span class="line">    emailAddress: <span class="string">&#x27;&#x27;</span>,  <span class="comment">// 设置默认值为空字符串</span></span><br><span class="line">    <span class="comment">//  使用正则表达式判断邮箱格式，如果正确则返回true反之返回false</span></span><br><span class="line">    isValid: Ember.computed.match(<span class="string">&#x27;emailAddress&#x27;</span>, <span class="regexp">/^.+@.+\..+$/</span>),</span><br><span class="line">    <span class="comment">// 把计算属性isValid绑定到isDisabled上</span></span><br><span class="line">    isDisabled: Ember.computed.not(<span class="string">&#x27;isValid&#x27;</span>)  <span class="comment">//当`disabled=false`时按钮可用，所以正好需要取反</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>到此校验问题基本实现了，等待项目重启完成，可以看到默认状态下按钮不可用，并且当你输入的内容不符合邮箱格式时按钮也是不可用的，如果输入的内容是一个正确的邮箱那么此时按钮自动变为可用状态。不好截图，就不截图了！请读者自己试验！！</p>
<h2 id="添加Action到控制器"><a href="#添加Action到控制器" class="headerlink" title="添加Action到控制器"></a>添加Action到控制器</h2><p>目前为止，输入检验也完成了，但你输入正确邮箱后添加按钮并不会发生任何事实，输入的内容也没有保存。下面开始介绍如何处理界面输入的内容。<br><br>首先修改模板<code>index.hbs</code>，在模板中增加一个<code>{{action}}</code>标签，有关Action请看<a target="_blank" rel="noopener" href="https://guides.emberjs.com/v2.4.0/templates/actions/">Actions</a>。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-primary btn-lg btn-block&quot;</span> <span class="attr">disabled</span>=<span class="string">&#123;&#123;isDisabled&#125;&#125;</span> &#123;&#123;<span class="attr">action</span> &#x27;<span class="attr">saveInvitation</span>&#x27;&#125;&#125;&gt;</span>Request invitation<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>仅仅修改了模板中<code>&lt;button&gt;</code>标签，其他不变，保存等待项目重启，此时在界面输入正确的邮箱然后点击按钮你在浏览器的控制台看到如下错误信息：</p>
<p><img src="/image/blog-image/96.png" alt="错误信息"></p>
<p>能看到错误信息说明你的项目是正确的，因为我们并没有定义<code>saveInvitation</code>，在控制器<code>index</code>中增加这个Action的定义。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/controller/index.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    <span class="comment">// isDisabled: true, //设置默认值为true</span></span><br><span class="line"></span><br><span class="line">    emailAddress: <span class="string">&#x27;&#x27;</span>,  <span class="comment">// 设置默认值为空字符串</span></span><br><span class="line"></span><br><span class="line">    emailAddress: <span class="string">&#x27;&#x27;</span>,  <span class="comment">// 设置默认值为空字符串</span></span><br><span class="line">    <span class="comment">//  使用正则表达式判断邮箱格式，如果正确则返回true反之返回false</span></span><br><span class="line">    isValid: Ember.computed.match(<span class="string">&#x27;emailAddress&#x27;</span>, <span class="regexp">/^.+@.+\..+$/</span>),</span><br><span class="line">    <span class="comment">// 把计算属性isValid绑定到isDisabled上</span></span><br><span class="line">    isDisabled: Ember.computed.not(<span class="string">&#x27;isValid&#x27;</span>),  <span class="comment">//当`disabled=false`时按钮可用，所以正好需要取反</span></span><br><span class="line"></span><br><span class="line">    actions: &#123;</span><br><span class="line">        saveInvitation: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="comment">//  注意alert中字符串两边使用的是 `  不是单引号或者双引号</span></span><br><span class="line">            alert(<span class="string">`Saving of the following email address is in propgress: <span class="subst">$&#123;<span class="built_in">this</span>.get(<span class="string">&#x27;emailAddress&#x27;</span>)&#125;</span>`</span>);</span><br><span class="line">            <span class="comment">// 模拟保存操作</span></span><br><span class="line">            <span class="built_in">this</span>.set(<span class="string">&#x27;responseMessage&#x27;</span>, <span class="string">`Thank you! We&#x27;ve just saved your email address: <span class="subst">$&#123;<span class="built_in">this</span>.get(<span class="string">&#x27;emailAddress&#x27;</span>)&#125;</span>`</span>);</span><br><span class="line">            <span class="comment">//  情况输入框内容</span></span><br><span class="line">            <span class="built_in">this</span>.set(<span class="string">&#x27;emailAddress&#x27;</span>, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p><strong>注意</strong>：代码<code>alert</code>方法中并没有使用单引号或者是双引号囊括字符串“Saving of the following email address is in propgress: ${this.get(‘emailAddress’)}”而是使用“`”，这两者肯定是有区别的，前者直接把<code>$&#123;this.get(&#39;emailAddress&#39;)&#125;</code>当着字符串，后者会把<code>$&#123;this.get(&#39;emailAddress&#39;)&#125;</code>当着表达式，从运行结果就可以看出来了。<br><br>输入正确邮箱后点击按钮会得到如下截图结果：</p>
<p><img src="/image/blog-image/97.png" alt="结果"></p>
<p>直接弹出提示信息这种方式太暴力了，改一种提示方式，修改模板<code>index.hbs</code>，然后在注释掉控制器<code>index.js</code>中的<code>alert</code>语句。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123;! app/templates/index.hbs&#125;&#125;</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;jumbotron text-center&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Coming Soon<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>Don&#x27;t miss our launch date, request an invitation now.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;form-horizontal form-group form-group-lg row&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-xs-10 col-xs-offset-1 col-sm-6 col-sm-offset-1 col-md-5 col-md-offset-2&quot;</span>&gt;</span></span><br><span class="line">          <span class="comment">&lt;!-- &lt;input type=&quot;email&quot; class=&quot;form-control&quot; placeholder=&quot;Please type your e-mail address.&quot; autofocus=&quot;autofocus&quot;/&gt; --&gt;</span></span><br><span class="line">          &#123;&#123;input type=&quot;email&quot; value=emailAddress class=&quot;form-control&quot; placeholder=&quot;Please type your e-mail address.&quot; autofocus=&quot;autofocus&quot;&#125;&#125;</span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-xs-10 col-xs-offset-1 col-sm-offset-0 col-sm-4 col-md-3&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">button</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-primary btn-lg btn-block&quot;</span> <span class="attr">disabled</span>=<span class="string">&#123;&#123;isDisabled&#125;&#125;</span> &#123;&#123;<span class="attr">action</span> &#x27;<span class="attr">saveInvitation</span>&#x27;&#125;&#125;&gt;</span>Request invitation<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    &#123;&#123;! 显示提示信息&#125;&#125;</span><br><span class="line">    &#123;&#123;#if responseMessage&#125;&#125;</span><br><span class="line">     <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;alert alert-success&quot;</span>&gt;</span>&#123;&#123;responseMessage&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">   &#123;&#123;/if&#125;&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">   <span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>上述代码新引入了一个表达式<code>{{if}}</code>，顾名思义，这个表达式就是用于判断的。更多有关判断表达式的介绍请看<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/03/18/ember-js-ru-men-zhi-nan-zhi-jiu-handlebarstiao-jian-biao-da-shi/">Ember.js 入门指南之九handlebars条件表达式</a></p>
<p>等待项目重启完成，再次测试。输入正确格式的邮箱，点击按钮提交可以看到如下的结果：</p>
<p><img src="/image/blog-image/98.png" alt="友好提示信息"></p>
<p>主要看绿色提示信息，相对于前一种直接弹框提示方式友好多了！！！</p>
<p>好了，到此第二篇也完成了。内容比较多需要耐心才能看完，如果你能认真坚持看到这里相信你一定收获了很多！！</p>
<h2 id="家庭作业"><a href="#家庭作业" class="headerlink" title="家庭作业"></a>家庭作业</h2><p><strong>作业要求</strong></p>
<ol>
<li>一个邮件输入框<code>{{input}}</code>，需要校验不为空、校验邮箱格式</li>
<li>一个消息输入框<code>{{textarea}}</code>，需要校验不为空、输入信息长度不少于5</li>
<li>上述两个输入框的校验都通过才允许提交</li>
<li>提交成功后在界面显示提示信息</li>
</ol>
<p><strong>用到的组件和方法</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123;input type=<span class="string">&quot;email&quot;</span> value=emailAddress <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;form-control&quot;</span> placeholder=<span class="string">&quot;Please type your e-mail address.&quot;</span> autofocus=<span class="string">&quot;autofocus&quot;</span>&#125;&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123;textarea <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;form-control&quot;</span> placeholder=<span class="string">&quot;Your message. (At least 5 characters.)&quot;</span> rows=<span class="string">&quot;7&quot;</span> value=message&#125;&#125;</span><br></pre></td></tr></table></figure>

<p>判断长度不小于5用到的方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Ember.computed.gte(<span class="string">&#x27;propertyName&#x27;</span>, number);</span><br></pre></td></tr></table></figure>

<p>并且判断。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Ember.computed.and(<span class="string">&#x27;firstProperty&#x27;</span>, <span class="string">&#x27;secondProperty&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>获取属性值的长度。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">message.length</span><br></pre></td></tr></table></figure>

<p>获取计算属性值长度</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Ember.computed(<span class="string">&#x27;propertyName&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.get(<span class="string">&#x27;propertyName&#x27;</span>).get(<span class="string">&#x27;length&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>


<br>
为了照顾懒人我把完整的代码放在[GitHub](https://github.com/ubuntuvim/library-app)上，如有需要请参考参考。博文经过多次修改，博文上的代码与github代码可能有出入，不过影响不大！如果你觉得博文对你有点用，请在github项目上给我点个`star`吧。您的肯定对我来说是最大的动力！！

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/Ember-Demo/%E6%A8%A1%E5%9E%8B%E9%AB%98%E7%BA%A7%E7%89%B9%E6%80%A7%EF%BC%8C%E5%BC%95%E5%85%A5%E6%A8%A1%E5%9E%8B%E5%85%B3%E8%81%94%E5%85%B3%E7%B3%BB/" rel="next" title="模型高级特性，引入模型关联关系">
          模型高级特性，引入模型关联关系
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/Ember-Demo/%E6%A8%A1%E5%9E%8B%EF%BC%8C%E4%BF%9D%E5%AD%98%E6%95%B0%E6%8D%AE%E5%88%B0%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="prev" title="模型，保存数据到数据库">
            模型，保存数据到数据库
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BE%8E%E5%8C%96%E4%B8%BB%E9%A1%B5%EF%BC%8C%E5%A2%9E%E5%8A%A0%E9%82%AE%E4%BB%B6%E8%BE%93%E5%85%A5%E6%A1%86"><span class="toc-text">美化主页，增加邮件输入框</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8%E9%A6%96%E9%A1%B5index-hbs%E4%B8%AD%E5%A2%9E%E5%8A%A0%E9%9D%99%E6%80%81HTML%E4%BB%A3%E7%A0%81"><span class="toc-text">在首页index.hbs中增加静态HTML代码</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7"><span class="toc-text">计算属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E4%BD%BF%E7%94%A8"><span class="toc-text">计算属性使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#isDisabled%E5%B1%9E%E6%80%A7"><span class="toc-text">isDisabled属性</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%A8%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E4%BF%AE%E6%94%B9isDisabled"><span class="toc-text">用计算属性修改isDisabled</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#isValid"><span class="toc-text">isValid</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0Action%E5%88%B0%E6%8E%A7%E5%88%B6%E5%99%A8"><span class="toc-text">添加Action到控制器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%B6%E5%BA%AD%E4%BD%9C%E4%B8%9A"><span class="toc-text">家庭作业</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '引入计算属性、action、动态内容', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
